package apis

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
	"strings"
	"tuowei.com/app/admin/models"
	"tuowei.com/app/admin/service"
	"tuowei.com/app/admin/service/dto"
	"tuowei.com/common/actions"
	"tuowei.com/common/global"
)

type SysAcademy struct {
	api.Api
}

// GetPages 列表院校推荐信息数据
// @Summary 列表院校推荐信息数据
// @Description 获取JSON
// @Tags 院校推荐
// @Param username query string false "username"
// @Success 200 {string} {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/sys-consult-lbt [get]
// @Security Bearer
func (e SysAcademy) GetPages(c *gin.Context) {
	s := service.SysAcademy{}
	req := dto.SysAcademyGetPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.Default(c.Request.Method, c.ContentType())).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	//规避直辖市
	req.Province = strings.Replace(req.Province, "市", "", -1)
	//数据权限检查
	p := actions.GetPermissionFromContext(c)

	list := make([]models.SysAcademy, 0)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(500, err, "查询失败")
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Insert
// @Summary 添加轮播图
// @Description 获取JSON
// @Tags 轮播图
// @Accept  application/json
// @Product application/json
// @Param data body dto.SysConsultLBTInsertReq true "轮播图数据"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/sys-consult-lbt [post]
// @Security Bearer
func (e SysAcademy) Insert(c *gin.Context) {
	s := service.SysAcademy{}
	req := dto.SysAcademyInsertReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	// 设置创建人
	req.SetCreateBy(user.GetUserId(c), user.GetUserName(c))
	err = s.Insert(&req)
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	e.OK(req.GetId(), "创建成功")
}

func (e SysAcademy) Change(c *gin.Context) {
	s := service.SysAcademy{}
	req := dto.SysAcademyUpdateReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	req.SetUpdateBy(user.GetUserId(c), user.GetUserName(c))
	err = s.Update(&req)
	if err != nil {
		e.Error(500, err, "更新失败")
		return
	}
	e.OK(req.GetId(), "更新成功")
}

// Delete
// @Summary 删除轮播图
// @Description 删除数据
// @Tags 轮播图
// @Param data body dto.SysConsultLBTDeleteReq true "body"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/sys-consult-lbt [delete]
// @Security Bearer
func (e SysAcademy) Delete(c *gin.Context) {
	s := new(service.SysAcademy)
	req := dto.SysAcademyDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, fmt.Sprintf("删除角色 %v 失败，\r\n失败信息 %s", req.Ids, err.Error()))
		return
	}

	err = s.Remove(&req)
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, "")
		return
	}
	_, err = global.LoadPolicy(c)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除角色 %v 失败，失败信息 %s", req.GetId(), err.Error()))
		return
	}
	e.OK(req.GetId(), fmt.Sprintf("删除角色角色 %v 状态成功！", req.GetId()))
}
